package view.type;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import entity.BookType;
import service.book.BookTypeServer;
import service.book.BookTypeServerImpl;
import utils.ComonUtil;
import view.CommonView.CommonJPanel;
import view.book.EditBookView;
import view.model.BookTypeTableModel;
import view.user.EditUserView;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 修改类别模块
 *
 */
public class EditTypeView extends CommonJPanel {
	

    //创建表格
	private JTable table;
	private static BookTypeTableModel bookTypeTableModel;//表格模板

    //创建搜索域文本框
	private JTextField editTypeField;

    //创建显示区域文本框
	private JTextField idField,typeNameField;
	private JTextArea descField;

    //创建按钮
    private JButton edit,delete,searchButton;
    
    private BookTypeServer bookTypeServer = new BookTypeServerImpl();

	public EditTypeView(){
		
		super(TYPE_TRANSPARENT);
		setLayout(new FlowLayout(FlowLayout.CENTER, 100, 50));
		
		


        //标题、输入框、按钮面板
        JPanel titlePanel = new CommonJPanel(TYPE_TRANSPARENT);
        titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 0));
        add(titlePanel);

        //创建内容标签
        JLabel labelEditType = new JLabel("图书类别名称：");
        labelEditType.setFont(new Font("微软雅黑", Font.BOLD, 20));
        titlePanel.add(labelEditType);
        //创建输入框
        editTypeField = new JTextField();
        editTypeField.setPreferredSize(new Dimension(200, 40));
        editTypeField.setFont(new Font("微软雅黑", Font.BOLD, 20));
        titlePanel.add(editTypeField);
        //创建按钮
        searchButton = new JButton("查询");
        searchButton.setPreferredSize(new Dimension(130, 40));
        titlePanel.add(searchButton);


        //创建表格面板
        JPanel tablePanel = new CommonJPanel(TYPE_TRANSPARENT);
        add(tablePanel);

        //创建表格
        bookTypeTableModel = new BookTypeTableModel();
        table = new JTable(bookTypeTableModel);
        JScrollPane sp = new JScrollPane(table);
        sp.setPreferredSize(new Dimension(600, 200));
        sp.setBackground(new Color(246, 246, 246));
        add(sp);


        //数据显示区域面板
        JPanel editPanel = new CommonJPanel(TYPE_TRANSPARENT);
        editPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
        add(editPanel);

        //创建编号标签、文本框
        JLabel labelId = new JLabel("编号：");
        editPanel.add(labelId);
        //创建编号文本框
        idField = new JTextField();
        idField.setEditable(false);
        idField.setPreferredSize(new Dimension(200, 30));
        editPanel.add(idField);

        //创建图书类别名称标签、文本框
        JLabel labelTypeName = new JLabel("图书类别名称：");
        editPanel.add(labelTypeName);
        //创建图书类别名称文本框
        typeNameField = new JTextField();
        typeNameField.setPreferredSize(new Dimension(200, 30));
        editPanel.add(typeNameField);


        //创建描述面板
        JPanel descPanel = new CommonJPanel(TYPE_TRANSPARENT);
        descPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
        add(descPanel);

        //创建描述标签、文本框
        JLabel labelDesc = new JLabel("描述：");
        descPanel.add(labelDesc);
        //创建图书类别名称文本框
        descField = new JTextArea();
        descField.setPreferredSize(new Dimension(550, 60));
        descField.setBackground(new Color(246, 246, 246));
        descPanel.add(descField);


        //创建按钮面板
        JPanel buttonPanel = new CommonJPanel(TYPE_TRANSPARENT);
        buttonPanel.setLayout(new GridLayout(1, 2, 80, 0));
        buttonPanel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        add(buttonPanel);

        //创建登录按钮
        edit = new JButton("修改");
        buttonPanel.add(edit);

        //创建重置按钮
        delete = new JButton("删除");
        buttonPanel.add(delete);


   

        //表格点击事件
        addTableListener();

        //修改按钮事件
        addModifyListener();

        //删除按钮事件
        addDeleteListener();
	
	}
	
    /**
     * @ 修改按钮事件
     */
    public void addModifyListener() {
    	 //添加监听器
        //搜索按钮
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            	updateData();
				resetInput();
            }
        });
        edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            
            	
            	
                if(ComonUtil.isEmpty(idField.getText())
                	||ComonUtil.isEmpty(typeNameField.getText())
                	|| ComonUtil.isEmpty(descField.getText())) {
                    JOptionPane.showMessageDialog(EditTypeView.this, "内容不能为空，请先选择一条记录！");
                    return;
                }

                
                BookType bookType = new BookType();
                bookType.setId(Integer.parseInt(idField.getText()));
                bookType.setBookTypeDesc(descField.getText());
                bookType.setBookTypeName(typeNameField.getText());
                
                String flag ="更新失败！";
				if(bookTypeServer.upBookType(bookType)) {
					flag = "更新成功！";
					updateData();
				}
				JOptionPane.showMessageDialog(EditTypeView.this, flag);

            }
        });
    }

    /**
     * 删除按钮事件
     */
    public void addDeleteListener() {
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            	
            	if (0 == idField.getText().length()) {
					JOptionPane.showMessageDialog(EditTypeView.this, "请先选择一条记录！");
					return;
				}
            	
            	int choice = JOptionPane.showConfirmDialog(EditTypeView.this, "是否删除？");
				if (choice != JOptionPane.OK_OPTION) {
					return;
				}
            	
				String flag ="删除失败！";
				if(bookTypeServer.deleteBookType(Integer.parseInt(idField.getText()))) {
					flag = "删除成功！";
					updateData();
					resetInput();
				}
				JOptionPane.showMessageDialog(EditTypeView.this, flag);
            }
        });
    }

    /**
     * 表格点击事件
     */
    public void addTableListener() {
        //表格点击事件
        table.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                BookType bookType = getSelectBookType();

                //将数据放入到表格中
                idField.setText(String.valueOf(bookType.getId()));
                typeNameField.setText(bookType.getBookTypeName());
                descField.setText(bookType.getBookTypeDesc());
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }
    
    /**
	 * 更新数据
	 */
	public void updateData() {
		BookType bookType = new BookType();
		bookType.setBookTypeName(editTypeField.getText());
		BookTypeTableModel.cs =  bookTypeServer.getBookTypeList(bookType);
		table.updateUI();
//		resetInput();
	}

    /**
     * @ 获取表格选中当前行的数据
     * @return
     */
    public BookType getSelectBookType() {
      int index = table.getSelectedRow();
      return BookTypeTableModel.cs.get(index);
  }


    /**
     * @ 重置输入框
     */
    public void resetInput() {
//        editTypeField.setText("");
        idField.setText("");
        typeNameField.setText("");
        descField.setText("");
    }
}
